package cn.com.inossem.db;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.content.Context;
import android.content.res.Resources.NotFoundException;
import android.os.Environment;

/**
 * 
 * @author ��Ӣ��
 *
 */
public class DBUtils {

	// private boolean flagMain;

	private static Context context = null;

	public static Context getContext() {
		return context;
	}

	public static void setContext(Context context) {
		DBUtils.context = context;
	}

	private static final String PATH_STRING = String.valueOf(Environment
			.getDataDirectory().getAbsolutePath())
			+ "/data/cn.com.inossem.activity/databases";

	// ����Դ�ļ��н���ݿ�ṹ����Ӧ��
	public static void copyDB(Context context, int resId) {

		File file = new File(PATH_STRING);
		InputStream inputStream = null;
		if (!file.exists()) {
			file.mkdirs();
			try {
				inputStream = context.getResources().openRawResource(resId);
				byte[] reader = new byte[inputStream.available()];
				while (inputStream.read(reader) != -1) {
					writefile(reader, PATH_STRING + "/sapdb");
				}
			} catch (NotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				if (inputStream != null) {
					try {
						inputStream.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}

		}
	}

	private static void writefile(byte[] str, String path) {
		File file;
		FileOutputStream out;
		try {
			file = new File(path);
			file.createNewFile();
			out = new FileOutputStream(file);

			out.write(str);
			out.close();
		} catch (IOException e) {
			e.printStackTrace();

		}
	}

}
